<?php /*
	This is the loop, which fetches entries from your database. It is used in some
	form on most of the K2 pages. Because of that, to make editing everything in one go
	easier, it has been placed in its own file, which is then included where needed.
	*/

	// Prepare Rolling Archives
	if ($_GET["rollingarchives"] == '1' or $_GET["livesearch"] == '1') { require("../../../wp-blog-header.php"); }
	
	// echo $wp_query->request;
	// print_r($wp_rewrite->rewrite_rules());
	
	// Asides Segregation
	$k2asidescategory = get_option('k2asidescategory');
	$k2asidesposition = get_option('k2asidesposition');

	if ( ($k2asidescategory != '0') && ($k2asidesposition == '1') && (is_home()) ) {

		if ( is_paged() ) {
		query_posts('cat=-' . $k2asidescategory . '&paged=' . $paged);
		} else {
		query_posts('cat=-' . $k2asidescategory);
		}
	}

	// Prepare Loop
 	if (have_posts()) {
?>

	<?php /* Headlines for archives */ if (!is_single() && !is_home() or is_paged()) { ?>

	<div class="pagetitle">
		<h2>
		<?php if (is_category()) {
		$k2asidescategory = get_option('k2asidescategory');
		if (the_category_ID(false) != $k2asidescategory) { ?> <?php printf(__('Archive for the \'%s\' Category','k2_domain'), single_cat_title('', false)) ?> <?php } else echo single_cat_title(); ?>
		
		<?php } elseif (is_day()) { ?>
		<?php printf(__('Archive for %s','k2_domain'), get_the_time(__('F jS, Y','k2_domain')))?>

		<?php } elseif (is_month()) { ?>
		<?php printf(__('Archive for %s','k2_domain'), get_the_time(__('F, Y','k2_domain'))) ?>

		<?php } elseif (is_year()) { ?>
		<?php printf(__('Archive for %s','k2_domain'), get_the_time(__('Y','k2_domain'))) ?>

		<?php } elseif (is_search()) { ?>
		<?php printf(__('Search Results for \'%s\'','k2_domain'), $s) ?>
		
		<?php } elseif (function_exists('is_tag') and is_tag()) { ?>
		<?php printf(__('Tag Archive for \'%s\'','k2_domain'), get_query_var('tag') ) ?>

		<?php } elseif (is_author()) { ?>

		<?php $post = $wp_query->post; $the_author = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = '$post->post_author' AND meta_key = 'nickname'");
		      printf(__('Author Archive for %s','k2_domain'), $the_author ) 
		?> 
		
		<?php // } elseif (is_paged()) { ?>
		<?php printf(__('Archive Page %s','k2_domain'), $paged ) ?>
		
		<?php } ?>
		</h2>
	</div>

	<?php } ?>
	

	<?php if (get_option('k2rollingarchives') == 1 and $_GET["rollingarchives"] != 1 and $_GET["livesearch"] != 1 and is_home()) { include (TEMPLATEPATH . '/rollingarchive.php'); } ?>

	<?php if (get_option('k2rollingarchives') == 0 and !is_single() and !is_home() and is_paged()) include (TEMPLATEPATH . '/navigation.php'); ?> 

	<?php if ($_GET["rollingarchives"] != '1' and $_GET["livesearch"] != '1') { ?><div id="dynamiccontent"></div><?php } ?>

	<div id="primarycontent"><div>

	<?php /* Start The Loop */ while (have_posts()) { the_post(); ?>
		
	<?php /* Permalink navigation has to be inside the loop */ if (is_single()) include (TEMPLATEPATH . '/navigation.php'); ?>
	
		<?php 
		/* Asides -- Pick a category to be an 'aside' in the K2 options panel */
		/* On archive pages, show asides inline no matter what */ 
		if ( is_archive() or is_search() or is_single() or (function_exists('is_tag') and is_tag()) ) {
			$k2asidescheck = '0';
		} else {
			$k2asidescheck = get_option('k2asidesposition');
		}
		
		$k2asidescategory = get_option('k2asidescategory');

		if ( ($k2asidescategory != '0') and (in_category($k2asidescategory) and ($k2asidescheck != '1')) ) {
		?>
	
			<div class="item aside">
				<div class="itemhead">
					<h3 id="post-<?php the_ID(); ?>" <?php /* Support for noteworthy plugin */ foreach((get_the_category()) as $cat) { if ($cat->cat_name == 'Noteworthy') { ?> class="noteworthy"<?php } } ?>><a href="<?php the_permalink() ?>" rel="bookmark" title='<?php printf(__('Permanent Link to "%s"','k2_domain'), strip_tags(get_the_title())) ?>'><?php the_title(); ?></a></h3>
					<?php /* Support for noteworthy plugin */ get_currentuserinfo(); global $user_level; if (function_exists(nw_noteworthyLink) and ($user_level == 10) ) { ?><?php nw_noteworthyLink($post->ID); ?><?php } ?>
					
					<?php edit_post_link(__('Edit','k2_domain'), '<span class="editlink">','</span>'); ?>

					<small class="metadata">
						<?php comments_popup_link('0&nbsp;<span>'.__('Comments','k2_domain').'</span>', '1&nbsp;<span>'.__('Comment','k2_domain').'</span>', '%&nbsp;<span>'.__('Comments','k2_domain').'</span>', 'commentslink', '<span class="commentslink">'.__('Closed','k2_domain').'</span>'); ?>

					<span class="chronodata">
					<?php
					$count_users = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->usermeta WHERE `meta_key` = '" . $table_prefix . "user_level' AND `meta_value` > 1");
					printf(__('Published %1$s %2$s.','k2_domain'),
						(($count_users > 1) ? sprintf(__('by %s','k2_domain'), '<a href="' . get_author_link(0, $authordata->ID, $authordata->user_nicename) .'">' . get_the_author() . '</a>') : ('')),
  						(function_exists('time_since') ? sprintf(__('%s ago','k2_domain'), time_since(abs(strtotime($post->post_date_gmt . " GMT")))) : get_the_time(__('F jS, Y','k2_domain')))
					);
					?>
					</span>                   
						<?php if (is_single() && function_exists(UTW_ShowTagsForCurrentPost)) { ?>
							<span class="tagdata"><?php _e('Tags:','k2_domain'); ?> <?php UTW_ShowTagsForCurrentPost("commalist") ?>.</span>
						<?php } ?>
					</small>
				</div>

				<div class="itemtext">
					<?php the_content(__('Continue reading','k2_domain') . " '" . the_title('', '', false) . "'"); ?>
				</div>

			</div>

		<?php  /* Normal Entries */ } elseif (!(in_category($k2asidescategory))) { ?>

			<div class="item entry">
				<div class="itemhead">
					<h3 id="post-<?php the_ID(); ?>" <?php /* Support for noteworthy plugin */ foreach((get_the_category()) as $cat) {  if ($cat->cat_name == 'Noteworthy') { ?> class="noteworthy"<?php } } ?>><a href="<?php the_permalink() ?>" rel="bookmark" title='<?php printf(__('Permanent Link to "%s"','k2_domain'), strip_tags(get_the_title())) ?>'><?php the_title(); ?></a></h3>
					<?php /* Support for noteworthy plugin */ get_currentuserinfo(); global $user_level; if (function_exists(nw_noteworthyLink) and ($user_level == 10) ) { ?><?php nw_noteworthyLink($post->ID); ?><?php } ?>

					<small class="metadata">
						<?php comments_popup_link('0&nbsp;<span>'.__('Comments','k2_domain').'</span>', '1&nbsp;<span>'.__('Comment','k2_domain').'</span>', '%&nbsp;<span>'.__('Comments','k2_domain').'</span>', 'commentslink', '<span class="commentslink">'.__('Closed','k2_domain').'</span>'); ?>

					<span class="chronodata">
					<?php
					$count_users = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->usermeta WHERE `meta_key` = '" . $table_prefix . "user_level' AND `meta_value` > 1");
					printf(__('Published %1$s %2$s %3$s.','k2_domain'), 
						(($count_users > 1) ? sprintf(__('by %s','k2_domain'), '<a href="' . get_author_link(0, $authordata->ID, $authordata->user_nicename) .'">' . get_the_author() . '</a>') : ('')),
						(function_exists('time_since') ? sprintf(__('%s ago','k2_domain'), time_since(abs(strtotime($post->post_date_gmt . " GMT")))) : get_the_time(__('F jS, Y','k2_domain'))),
						(sprintf(__('in %s','k2_domain'), function_exists('the_nice_category') ? the_nice_category(', ') : get_the_category_list(', ')))
					);
					?>
					</span>
						<?php edit_post_link(__('Edit','k2_domain'), '<span class="editlink">','</span>'); ?>
						<?php if (is_single() && function_exists(UTW_ShowTagsForCurrentPost)) { ?>
							<span class="tagdata"><?php _e('Tags:','k2_domain'); ?> <?php UTW_ShowTagsForCurrentPost("commalist") ?>.</span>
						<?php } ?>
					</small>
				</div>
	
				<div class="itemtext">
					<?php if ( is_archive() or is_search() or (function_exists('is_tag') and is_tag()) ) {
						the_excerpt();
					} else {
						the_content(__('Continue reading','k2_domain') . " '" . the_title('', '', false) . "'");
					} ?>
	
					<?php link_pages('<p><strong>'.__('Pages:','k2_domain').'</strong> ', '</p>', __('number','k2_domain')); ?>
				</div>

				<!--
				<?php trackback_rdf(); ?>
				-->
			</div>
				
		<?php /* End Asides Segregation Code */ }
	} /* End The Loop */ ?>
	
	</div></div>
	
	<?php /* Insert Paged Navigation */ if (!is_single() && get_option('k2rollingarchives') != 1) { include (TEMPLATEPATH.'/navigation.php'); } ?>

<?php /* If there is nothing to loop */  } else { $notfound = '1'; /* So we can tell the sidebar what to do */ ?>

	<div class="center">
		<h2><?php _e('Not Found','k2_domain'); ?></h2>
	</div>

	<div class="item">
	<div class="itemtext">
		<p><?php _e('Oh no! You\'re looking for something which just isn\'t here! Fear not however, errors are to be expected, and luckily there are tools on the sidebar for you to use in your search for what you need.','k2_domain'); ?></p>
	</div>
	</div>

<?php /* End Loop Init  */ } ?>
